package com.key.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.AssetChangeAdvDTO;
import com.key.common.dto.AssetChangeDTO;
import com.key.common.vo.AssetChangeUpdateVO;
import com.key.common.vo.ChangeAssetsListVO;
import com.key.entity.*;
import com.key.mapper.AssetChangeMapper;
import com.key.mapper.ShowBackMapper;
import com.key.service.AssetChangeService;
import com.key.utils.ExcelExportHelper;
import com.key.utils.ShowBackUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*
 * @description:实物信息变更相关业务实现类
 * @author: jianjiawei 
 * @date: 2023/2/20 9:40
 **/
@Service
public class AssetChangeServiceImpl implements AssetChangeService {

    @Autowired
    private AssetChangeMapper acMapper;
    @Autowired
    private ShowBackUtils showBackUtils;
    @Autowired
    private ShowBackMapper showBackMapper;

    /*
     *
     * @description:新建实物信息变更的表单数据回显
     * @author: jianjiawei
     * @date: 2023/2/20 10:42
     **/
    @Override
    public R showAssetsChange() {
        //生成变更单号
        AssetChange assetChange = acMapper.selectLastOne();
        Integer num = 10000001 + assetChange.getId();
        String changeNo = "ac" + num;
        List<String> companies = showBackUtils.showCompanies();
        List<String> types = showBackUtils.showAssetTypes();
        List<String> models = showBackUtils.showAssetModels();
        List<String> regions = showBackUtils.showRegions();
        List<String> users = showBackUtils.showUsers();
        Map map = new HashMap();
        map.put("changeNo", changeNo);
        map.put("companyList", companies);
        map.put("classList", types);
        map.put("modelList", models);
        map.put("userList", users);
        map.put("regionList", regions);
        String data = JSONObject.toJSONString(map);
        System.out.println(data);
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(data);
        return r;
    }

    /*
     *
     * @description:创建新的实物信息变更清单
     * @author: jianjiawei
     * @date: 2023/2/20 14:16
     **/
    @Override
    @Transactional
    public R saveAssetChange(AssetChange assetChange) {
        R r = new R();
        assetChange.setChangeState("未办理");
        int row = acMapper.InsertAssetChange(assetChange);
        if (row > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    /*
    *
     * @description:查询实物信息变更列表
     * @author: jianjiawei 
     * @date: 2023/2/20 15:51
     **/
    @Override
    public R showAssetsChangeList(Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage,pageSize);
        R r = new R();
        List<AssetChange> assetChanges = acMapper.selectAssetChangeList();

        for (AssetChange assetChange : assetChanges) {
            assetChange.setAssetsList(this.showChangeAssetsList(assetChange.getId()));
        }
        if (assetChanges != null & assetChanges.size() > 0){
            String data = JSONObject.toJSONString(assetChanges);
            r.setCode(200);
            r.setMsg("success");
            r.setData(data);
        }else {
            r.setCode(200);
            r.setMsg("没有满足条件的数据");
        }
        return r;
    }

    /*
    *
     * @description:修改实物信息变更的信息回显
     * @author: jianjiawei
     * @date: 2023/2/20 16:29
     **/
    @Override
    public R showUpdateAssetsChange(Integer id) {
        AssetChange assetChange = acMapper.selectAssetChangeById(id);
        List<ChangeAssetsListVO> listVOS = this.showChangeAssetsList(id);
        AssetChangeUpdateVO vo = new AssetChangeUpdateVO(assetChange.getId(),assetChange.getChangeNo(),assetChange.getChangeDate(),assetChange.getAssetIds(),assetChange.getAssetClassNew(),
                assetChange.getAssetModelNew(),assetChange.getAssetName(),assetChange.getAssetBuyDateNew(),assetChange.getAssetSourceNew(),assetChange.getAssetUserNew(),
                assetChange.getAssetUserCompNew(),assetChange.getAssetLifeNew(),assetChange.getAssetSupplierNew(),assetChange.getAssetRegionNew(),assetChange.getRemarks(),listVOS);
        String data = JSONObject.toJSONString(vo);
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(data);
        return r;
    }

    /*
    *
     * @description:实物信息变更中修改保存
     * @author: jianjiawei
     * @date: 2023/2/20 16:51
     **/
    @Override
    @Transactional
    public R updateAssetChangeSave(AssetChangeDTO acDTO){
        int row = acMapper.updateAssetChangeSave(acDTO);
        if (row > 0){
            //获取型号ID
            List<AssetModel> assetModels = showBackMapper.showAssetModels();
            Integer assetModelId = 0;
            for (AssetModel model : assetModels) {
                if (model.getModelName().equals(acDTO.getAssetModelNew())){
                    assetModelId = model.getId();
                }
            }
            //获取区域ID
            List<CArea> cAreas = showBackMapper.showRegions();
            Integer regionId = 0;
            for (CArea cArea : cAreas) {
                if (cArea.getCAreaName().equals(acDTO.getAssetRegionNew())){
                    regionId = cArea.getId();
                }
            }
            //获取公司ID
            List<Company> companies = showBackMapper.showCompanies();
            Integer companyId = 0;
            for (Company company : companies) {
                if (company.getCompanyName().equals(acDTO.getChangeBelongComp())){
                    companyId = company.getCompanyId();
                }
            }
            //获取使用人ID
            List<Employee> employees = showBackMapper.showUsers();
            Integer userId = 0;
            for (Employee employee : employees) {
                if (employee.getEmployeeName().equals(acDTO.getAssetUserNew())){
                    userId = employee.getEmployeeId();
                }
            }

             //根据变更单号查询assetIds
                AssetChange assetChange = acMapper.selectAssetChangeByChangeNo(acDTO.getChangeNo());
                String assetIds = assetChange.getAssetIds();
                String[] ids = assetIds.split(",");
            if (ids != null){
                for (String id : ids) {
                    Map map = new  HashMap();
                    map.put("assetsId",Integer.valueOf(id));
                    map.put("assetsModelId",assetModelId);
                    map.put("assetsName",acDTO.getAssetName());
                    map.put("regionId",regionId);
                    map.put("assetsPurchaseDate",acDTO.getAssetBuyDateNew());
                    map.put("assetsServiceLife",acDTO.getAssetLifeNew());
                    map.put("assetsSource",acDTO.getAssetSourceNew());
                    map.put("assetsAdminId",companyId);
                    map.put("assetsEmployeeId",userId);
                    map.put("assetsStorageLocation",acDTO.getAssetPlace());
                    map.put("assetsRemarks",acDTO.getRemarks());
                    acMapper.updateAssetsByChange(map);
                }
            }
        }

        R r = new R();
        if (row > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("修改失败");
        }

        return r;
    }

    /*
    *
     * @description:删除实物信息数据
     * @author: jianjiawei
     * @date: 2023/2/21 10:34
     **/
    @Override
    @Transactional
    public R deleteAssetsChange(Integer id) {
        int row = acMapper.deleteAssetChangeById(id);
        R r = new R();
        if (row > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }

    @Override
    public R showAcAdvSearch() {
        List<String> companies = showBackUtils.showCompanies();
        List<String> models = showBackUtils.showAssetModels();
        List<String> regions = showBackUtils.showRegions();
        List<String> users = showBackUtils.showUsers();
        Map map = new HashMap();
        map.put("companyList", companies);
        map.put("modelList", models);
        map.put("userList", users);
        map.put("regionList", regions);
        String data = JSONObject.toJSONString(map);
        System.out.println(data);
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(data);
        return r;
    }

    @Override
    public R findAssetsChangeListByAdv(AssetChangeAdvDTO advDTO) {
        PageHelper.startPage(advDTO.getCurrentPage(),advDTO.getPageSize());
        R r = new R();
        List<AssetChange> assetChanges = acMapper.selectAssetChangeByAdv(advDTO);
        if (assetChanges != null & assetChanges.size()>0){
            String data = JSONObject.toJSONString(assetChanges);
            r.setCode(200);
            r.setMsg("success");
            r.setData(data);
        }else {
            r.setCode(200);
            r.setMsg("没有满足条件的数据");
        }
        return r;
    }

    /*
    *
     * @description:回显实物信息变更资产列表
     * @author: jianjiawei
     * @date: 2023/2/21 15:38
     **/
    @Override
    public List<ChangeAssetsListVO> showChangeAssetsList(Integer acId) {
        AssetChange assetChange = acMapper.selectAssetChangeById(acId);
        String assetIds = assetChange.getAssetIds();
        List<ChangeAssetsListVO> listVO = new ArrayList<>();
        if (assetIds != null & assetIds != "") {
            String[] assetsIds = assetIds.split(",");
            for (String assetsId : assetsIds) {
                ChangeAssetsListVO changeAssetsListVO = acMapper.selectAssetsById(Long.parseLong(assetsId));
                changeAssetsListVO.setAssetsAdmin(acMapper.getAssetsAdmin(changeAssetsListVO.getAssetsId()));
                listVO.add(changeAssetsListVO);
            }
        }
        return listVO;
    }


    @Override
    public HSSFWorkbook getWorkbook() {
        String[] header = {"主键ID","变更单号","变更状态","业务日期","处理人","业务所属公司","资产id列表","资产类别","资产型号","资产名称",
                "资产区域","购入日期","使用期限","来源","供应商","使用公司","使用人","存放地方","使用部门","备注"};
        String[] properties = {"id","changeNo","changeState","changeDate","changeHandler","changeBelongComp","assetIds","assetClassNew","assetModelNew","assetName",
                "assetRegionNew","assetBuyDateNew","assetLifeNew","assetSourceNew","assetSupplierNew","assetUserCompNew","assetUserNew","assetPlace","assetDept","remarks"};
        List assetChanges = acMapper.selectAssetChangeList();
        ExcelExportHelper excelHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelHelper.exportExcel(header, properties, assetChanges, "实物信息变更");
        return workbook;
    }


}